﻿using Common;
using DadayunApi.Model;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace DadayunApi
{
    public partial class DataApi:Api
    {
        public DataApi(AppAuthBase authBase, string baseUri) : base(authBase, baseUri)
        {
        }

        /// <summary>
        /// 获取产品属性配置
        /// </summary>
        /// <returns>产品信息表的模板ID，产品编码字段ID，产品属性字段ID集</returns>
        public List<ProductPropertySet> GetProductPropertySets()
        {
            List<ProductPropertySet> list = null;
            var uri = "https://api.dadayun.cn/v1/data/product_fields";

            var ans = HttpClientHelper.HttpClientGetSync(baseUri, uri, out string json, httpgetTokenHeader);
            if (ans == HttpStatusCode.OK)
            {
                list = JsonConvert.DeserializeObject<List<ProductPropertySet>>(json);
            }
            return list;
        }
        /// <summary>
        /// 根据产品编码获取产品属性
        /// </summary>
        /// <param name="productNumber">产品编码</param>
        /// <param name="productNumberFieldId">产品编码字段ID</param>
        /// <param name="productFormId">产品信息表ID</param>
        /// <returns>属性ID和属性值的键值对</returns>
        public Dictionary<string, string> GetProductProperties(string productNumber, string productNumberFieldId, string productFormId)
        {
            Dictionary<string, string> list = null;
            var uri = "https://api.dadayun.cn/v1/data/product_properties_by_product_number";
            uri += "?productNumber=" + productNumber + "&productNumberFieldId=" + productNumberFieldId + "&productFormId=" + productFormId;
            var ans = HttpClientHelper.HttpClientGetSync(baseUri, uri, out string json, httpgetTokenHeader);
            if (ans == HttpStatusCode.OK)
            {
                list = JsonConvert.DeserializeObject<Dictionary<string, string>>(json);
            }
            return list;
        }
    }
}
